package com.laoyao.comm.config;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class WxUserIdLanjie implements Interceptor{
	public void intercept(Invocation inv) {
		 String tel="";
		 Integer userId=null;
		 if (inv.getController().getSessionAttr("user_tel")!=null) {
			tel=inv.getController().getSessionAttr("user_tel");
		 }
		 if (inv.getController().getSessionAttr("user_id")!=null) {
			 userId=inv.getController().getSessionAttr("user_id");
		 }
		 if((tel!=null && !tel.toString().equals(""))||(userId!=null && !userId.toString().equals(""))){
			 String sql="select * from user where user_tel=?  and user_state=0 and user_role=1";
			Record user=Db.findFirst(sql,tel);
			if (user!=null) {
				inv.getController().setSessionAttr("user_id", user.getInt("user_id"));
				inv.getController().setSessionAttr("user_tel", user.get("user_tel"));
				inv.invoke();//放过拦截
			}else {
					 inv.getController().redirect("/weix");
			}
		}else{
			inv.getController().redirect("/weix");
		}
	}

}
